|
|
|
|
|
|
|
Solution 5
Finding the Executable Name |
|
|
|
|
|
|
|
|
Did you find it difficult to discover the API error in this case? I hope sobecause the declaration is perfect. In fact, the function call is just fine as well. The problem is not in the API call, but in what we did with the result. |
|
|
|
|
|
|
|
|
Let's look at the code step by step. |
|
|
|
|
|
|
|
|
First, we initialize the string to 261 bytes: |
|
|
|
|
|
|
|
|
ExecName = String$(MAX_PATH + 1, 0) |
|
|
|
|
|
|
|
|
Figure S5-1:
Initial contents of the ExecName string |
|
|
|
|
|
|
|
|
The ExecName string now contains 261 characters, each with a 0 value. This is stored internally in BSTR format, meaning that Visual Basic stores the length of the string followed by the data, as shown in Figure S5-1. Note that the number 0 indicates that the character has the ASCII value 0, not that it contains the character ''0." |
|
|
|
|
|
|
|
|
Next, we load the file name using the GetModuleFileName API function: |
|
|
|
|
|
|
|
|
Call GetModuleFileName(0, ExecName, MAX_PATH) |
|
|
|
|
|
|
|
|
This loads the string with the full path to the application. Let's assume for this example that the program is running within the Visual Basic environment and that the program vb6.exe is located on the root directory of drive C. The string now contains the data shown in Figure S5-2. |
|
|
|
|
|
|
|
|
Figure S5-2:
Contents of the ExecName string after it is loaded by the
GetModuleFileName function |
|
|
|
|
|
|
|
|
The next step involves removing the path, since we need only the base executable name to determine if the program is Visual Basic. This is accomplished by searching from the end of the string backward for the backslash (\) character. In this example, the character is in position 3. The string is then reassigned using the Mid function to obtain the data after the \ character. |
|
|
|
|
|